/**
  ******************************************************************************
  * File Name          : sfspi.c
  * Description        : This file provides code for the configuration
  *                      of the SPI instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sfbsp.h"

/* SPI2 init function */
void sFlash_Init(void)
{
	spi_parameter_struct spi_init_struct;
	
	rcu_periph_clock_enable(RCU_GPIOB);
	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_SPI1);
	
	SFLASH_WP_HIGH();
	SFLASH_CS_HIGH();
	
	gpio_mode_set(SFLASH_WP_GPIO_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, SFLASH_WP_Pin);
	gpio_output_options_set(SFLASH_WP_GPIO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, SFLASH_WP_Pin);
	
	gpio_mode_set(SFLASH_CS_GPIO_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, SFLASH_CS_Pin);
	gpio_output_options_set(SFLASH_CS_GPIO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, SFLASH_CS_Pin);
	
	gpio_af_set(GPIOB, GPIO_AF_5, GPIO_PIN_13 | GPIO_PIN_14 |GPIO_PIN_15);
	gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13 | GPIO_PIN_14 |GPIO_PIN_15);
	gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_14 |GPIO_PIN_15);

  /* SPI1 parameter config */
	spi_init_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;
	spi_init_struct.device_mode          = SPI_MASTER;
	spi_init_struct.frame_size           = SPI_FRAMESIZE_8BIT;
	spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
	spi_init_struct.nss                  = SPI_NSS_SOFT;
	spi_init_struct.prescale             = SPI_PSC_4;
	spi_init_struct.endian               = SPI_ENDIAN_MSB;
	spi_init(SPI1, &spi_init_struct);
	
	spi_crc_off(SPI1);
	
	spi_ti_mode_disable(SPI1);
	
	spi_enable(SPI1);
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
